package com.lishe.lishe.lishe.module.mall.view.snapscrollview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.lishe.lishe.R;

/**
 * Created by Bin
 */
public class GoodsDetailPage implements SnapPageLayout.SnapPage, OnClickListener {

	private Context context;
	private View rootView = null;
	private WebView web;
	private ImageView mUP;
	//回到商品详情展示页 回调接口
	private UpClicker mClicker;

	public interface UpClicker{
		void onUpClick();
	}

	public GoodsDetailPage(Context context, View rootView, UpClicker clicker) {
		this.context = context;
		this.rootView = rootView;
		this.mClicker = clicker;
		initBottomView(rootView);
	}

	/**
	 * 下面的webview
	 */
	private void initBottomView(View bottomView) {
		mUP = bottomView.findViewById(R.id.up);
		mUP.setOnClickListener(this);
		web = bottomView.findViewById(R.id.web);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.up:
			web.scrollTo(0, 0);
			mClicker.onUpClick();
			break;
		}
	}

	@SuppressLint("SetJavaScriptEnabled")
	public void setWebView(String url) {
		/*WebSettings webSettings = web.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setAllowFileAccess(true);
		webSettings.setSupportMultipleWindows(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setDomStorageEnabled(true);
		webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
		webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
			webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
		}*/
		WebSettings webSettings = web.getSettings();
		webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
		webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
		web.setWebViewClient(new MyWebViewClient());
		web.loadUrl(url);
	}

	private class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {//url拦截事件
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			imgReset();
		}
	}

	/**
	 * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
	 * http://blog.csdn.net/smileiam/article/details/72123227,资料来源
	 **/
	private void imgReset() {
		web.loadUrl("javascript:(function(){" +
				"var objs = document.getElementsByTagName('img'); " +
				"for(var i=0;i<objs.length;i++)  " +
				"{"
				+ "var img = objs[i];   " +
				"    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
				"}" +
				"})()");
	}

	@Override
	public View getRootView() {
		return rootView;
	}

	@Override
	public boolean isAtTop() {
		if (web.getScrollY() == 0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean isAtBottom() {
		return false;
	}

}
